import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);
const state = {
  tokenCookie: JSON.parse(localStorage.getItem("tokenCookie")) || null,
  user: JSON.parse(localStorage.getItem("user")) || "",
  theme: localStorage.getItem("theme") || "#409eff",
  themeColor: localStorage.getItem("themeColor") || "#409eff",
  playlist: [], //播放列表
  playHistory: [], // 播放历史
  playContent: "", //当前播放的歌曲信息
  current: "", //当前歌曲的播放进度
  lyricContent: []
};
const mutations = {
  handleUser(state, userinfo) {
    state.tokenCookie = {
      token: userinfo.token,
      cookie: userinfo.cookie
    };
    state.user = userinfo.profile;
    localStorage.setItem("tokenCookie", JSON.stringify(state.tokenCookie));
    localStorage.setItem("user", JSON.stringify(userinfo.profile));
    return userinfo;
  },
  removeLogin(state, user) {
    state.user = user;
    localStorage.removeItem("tokenCookie");
    localStorage.removeItem("user");
  },
  themeColor(state, themeColor) {
    state.themeColor = themeColor;
    localStorage.setItem("themeColor", themeColor);
  },
  setplaylist(state, payload) {
    state.playlist = payload;
  },
  //获取当前播放歌曲的信息
  setContent(state, payload) {
    // console.log(state.playHistory);
    state.playContent = payload;
    state.playHistory.indexOf(payload) == -1
      ? state.playHistory.unshift(payload)
      : "";

    state.playlist.indexOf(payload) == -1
      ? state.playlist.unshift(payload)
      : "";
    // localStorage.setItem("playHistory", JSON.stringify(state.playHistory));
  },
  //获取当前播放歌曲的歌词
  setLyric(state, payload) {
    state.lyricContent = payload;
  },
  //设置当前播放歌曲的进度
  setCurrent(state, payload) {
    // console.log(payload);
    state.current = Math.ceil(payload);
  },
  //从播放列表中移除歌曲
  removeSong(state, payload) {
    state.playlist = payload;
    // let index = state.playList.indexOf(payload);
    // state.playList.splice(index, 1);
  },
  //历史记录中移除歌曲
  removeHistory(state, payload) {
    state.playHistory = payload;
  }
  //往播放列表中加入歌曲
  // addSong(state, payload) {
  //   let index = state.playList.indexOf(payload);
  //   state.playList.indexOf(payload) == -1 ? state.playList.push(payload) : "";
  // }
};
const actions = {};

const modules = {};
export default new Vuex.Store({
  state,
  mutations,
  actions,
  modules
});
